package sk.custom.button;

import java.awt.Canvas;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.FontMetrics;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.geom.Rectangle2D;

public class CButton extends Canvas implements MouseListener {
	
//	  public static final int UNARMED = 0;
//    public static final int ARMED = 1;
//    public static final int OVER = 2;
//    public static final int DISABLED = 3;
    
   
    String label;
    boolean pressed = false;
    int width, height, sX = -1, sY = -1, minWidth = 70;
    int dx = 7,dy = 4;
    Color borderColor = new Color(221, 221, 221);                            // initial rectangle borders color 
    Color interierColor = new Color(241, 242, 243);                          // initial rectangle interier color
    Color fontColor = new Color(113, 111, 100);                              // font color
    Color pressedBorderColor = new Color(102, 102, 102);                     // rectangle borders color after pressing mouse
    Color pressedInterierColor = new Color(221, 221, 221);                   // rectangle interier color after pressing mouse
    Color releasedBorderColor = new Color(221, 221, 221);                    // rectangle borders color after releasing mouse
    Color releasedInterierColor = new Color(241, 242, 243);                  // rectangle interier color after releasing mouse
    Color overColor = new Color(234, 246, 247);
    Color overBorderColor = new Color(184, 237, 241);
    FontMetrics fm = null;
    Rectangle2D r = null;
    Font font = null;

    protected void init(String label){
    	this.label = label;
        addMouseListener(this);    	
    }
    public CButton(String label){
    	init(label);
    }
    public CButton(String label, String fontName){
    	init(label);
        font = new Font(fontName, Font.PLAIN, 25);                	
    }
    public CButton(String label, Font font){
    	init(label);
    	this.font = font;
    }
    public void paint(Graphics gr) {    	
    	if(font != null) {
    	   gr.setFont(font);
    	}
    	fm = gr.getFontMetrics();
    	r = fm.getStringBounds(label, gr);

    	width = (int) r.getWidth() + 14;
    	if(width < minWidth){
    		width = minWidth;
    		dx = 19;
    	}
    	height = (int)r.getHeight() + 7;
  	    this.setSize(width+2, height+2);
//  	    this.setSize(width+1, height+1);
    	
    	if(sX == -1 && sY == -1){
    	   sX = Math.abs((int)r.getX()) + dx;    	
    	   sY = Math.abs((int)r.getY()) + dy;
    	}

    	
    	
    	gr.setColor(borderColor); 
//    	gr.drawRect(0, 0, width, height);
    	gr.drawRect(1, 1, width-1, height-1);
    	gr.setColor(interierColor);                  	
//    	gr.fillRect(1, 1, width-1, height-1);  
    	gr.fillRect(2, 2, width-2, height-2);  
    	gr.setColor(fontColor);               
    	gr.drawString(label, sX, sY);
/* start ohranicenie platna tenkou cervenou ciarou */    	
   // 	gr.setColor(new Color(255,0,0));
   // 	gr.drawRect(0,0,this.getBounds().width-1, this.getBounds().height-1);
/* end */    	
    	this.setEnabled(true);
    }
     public void mousePressed(MouseEvent e) {
    	pressed = true;
        setBorderColor(pressedBorderColor);
        setInterierColor(pressedInterierColor);
        setSX(getSX()+1);
        setSY(getSY()+1);
        repaint();
     }

     public void mouseReleased(MouseEvent e) {
    	 pressed = false;
         setBorderColor(releasedBorderColor);
         if(!this.contains(e.getX(), e.getY())){
        	 setInterierColor(releasedInterierColor);
         }else{
        	 setInterierColor(overColor); 
        	 setBorderColor(overBorderColor);  // test
         }
         setSX(getSX()-1);
         setSY(getSY()-1);
         repaint();    	 
     }

     public void mouseEntered(MouseEvent e) {
    	 setInterierColor(overColor);
    	 setBorderColor(overBorderColor);  // test
    	 repaint();
     }

     public void mouseExited(MouseEvent e) {
    	 if(pressed){
    	     setBorderColor(new Color(102, 102, 102)); 
    	     setInterierColor(new Color(221, 221, 221));    		     		 
    	 }else{
        	 setInterierColor(new Color(241, 242, 243)); 
             setBorderColor(new Color(221, 221, 221));   		 
    	 }
    	 repaint();
     }

     public void mouseClicked(MouseEvent e) {
    	 onClick(e);
     }
     public void setBorderColor(Color borderColor){
    	 this.borderColor = borderColor;
     }
     public void setInterierColor(Color interierColor){
    	 this.interierColor = interierColor;
     }
     public void setFontColor(Color fontColor){
    	 this.fontColor = fontColor;
     }
     public void setSX(int sX){
    	 this.sX = sX;
     }
     public int getSX(){
    	 return this.sX;
     }
     public int getSY(){
    	 return this.sY;
     }
     public void setSY(int sY){
    	 this.sY = sY;
     }
	 public void onClick(MouseEvent e){};
}
